const mongoose = require('mongoose');
// mongoose.set('useCreateIndex', true)
mongoose.connect('mongodb://localhost:27017/express-auth', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true
}, err => {
    if (err) {
        console.log('数据库连接失败');
    } else {
        console.log('数据库连接成功');
    }
});

const userSchema = new mongoose.Schema({
    username: {
        type: String,
        // 唯一性
        unique: true
    },
    password: {
        type: String,
        // 加密bcrypt
        set(val) {
            return require('bcryptjs').hashSync(val, 10);
        }
    }
});

const User = mongoose.model('User', userSchema);
// User.db.dropCollection('users'); 删除数据库
module.exports = { User };